package com.goodving.guli.bt;

import android.app.AlarmManager;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;

import com.goodving.guli.R;
import com.goodving.guli.bt.dataprocess.DaySportData;
import com.goodving.guli.bt.dataprocess.SleepInfo;
import com.goodving.guli.bt.dataprocess.SportDay;
import com.goodving.guli.bt.dataprocess.StepsInfo;
import com.goodving.guli.bt.lua.ActiveItem;
import com.goodving.guli.bt.lua.LuaEvent;
import com.goodving.guli.bt.model.DataManager;
import com.goodving.guli.bt.model.LabFactoryActiveItem;
import com.goodving.guli.bt.model.PersonInfo;
import com.goodving.guli.bt.model.ReportData;
import com.goodving.guli.bt.profile.scale.GuliLog;
import com.goodving.guli.bt.utils.C0893f;
import com.goodving.guli.model.DeviceSource;
import com.goodving.guli.model.Keeper;
import com.goodving.guli.utility.gulihardware.Constant;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;

import cn.com.smartdevices.bracelet.chart.util.ChartData;

/**
 * Created by john on 2015/12/17.
 */
public class DynamicManager {
    private static final int f3433a = 20;
    private static final int f3434b = 1290;
    private static final int f3435c = 630;
    private static final int f3436e = 300;
    private static DynamicManager f3437f;
    private final String f3438d;

    static {
        f3437f = null;
    }

    private DynamicManager() {
        this.f3438d = "DynamicManager";
    }

    private void m7893a(Context context, int i, int i2) {
        CharSequence string;
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (i >= i2) {
            string = context.getString(R.string.daily_sport_report_notify, new Object[]{Integer.valueOf(i)});
        } else {
            string = context.getString(R.string.daily_sport_report_notify_hurry, new Object[]{Integer.valueOf(i2 - i)});
        }
//        aT e = new aT(context).m647a((int) R.drawable.app_icon).m664a(string).m673b(context.getString(R.string.daily_sport_report_notify_content)).m687e(true);
//        e.m668a(new long[]{100, 500});
//        e.m654a(PendingIntent.getActivity(context, 0, new Intent(context, DailySportReportActivity.class), 268435456));
//        notificationManager.notify(C3299l.f17108a, e.m676c());
    }

    private void m7894a(Context context, String str) {
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
//        aT e = new aT(context).m647a((int) R.drawable.app_icon).m664a((CharSequence) str).m673b(context.getString(R.string.notify_sleep_content)).m687e(true);
//        e.m668a(new long[]{100, 500});
//        e.m654a(PendingIntent.getActivity(context, 0, new Intent(context, DailySleepReportActivity.class), 268435456));
//        notificationManager.notify(16, e.m676c());
    }

    private void m7895a(Context context, boolean z) {
        String readLastWeekReportDate = Keeper.readLastWeekReportDate();
        ReportData e = DataManager.m12179a().m12221e();
        if (e.isValid()) {
            GuliLog.tracePrintInfo("DynamicManager", e.toString());
            if (z || readLastWeekReportDate == null || !readLastWeekReportDate.equals(e.timeFrom)) {
                Keeper.KeepLastWeekReportDate(e.timeFrom);
                GuliLog.tracePrintInfo("DynamicManager", "set week report .........................");
                LuaEvent.getInstance(context).setWeekReport(e);
                return;
            }
            return;
        }
        GuliLog.tracePrintInfo("DynamicManager", "generateLastWeekReport inValid!!!!");
    }

    private void m7896b(Context context, boolean z) {
        if (Calendar.getInstance().get(Calendar.DATE) <= 10) {
            String readLastMonthReportDate = Keeper.readLastMonthReportDate();
            ReportData g = DataManager.m12179a().m12225g();
            if (g.isValid()) {
                GuliLog.tracePrintInfo("DynamicManager", g.toString());
                if (z || readLastMonthReportDate == null || !readLastMonthReportDate.equals(g.timeFrom)) {
                    Keeper.KeepLastMonthReportDate(g.timeFrom);
                    GuliLog.tracePrintInfo("DynamicManager", "set Month report .........................");
                    LuaEvent.getInstance(context).setMonthReport(g);
                    return;
                }
                return;
            }
            GuliLog.tracePrintInfo("DynamicManager", "generateLastMonthReport inValid!!!!!!!");
        }
    }

    private void m7897c(Context context, boolean z) {
//        ReportInfo readContinueReachGoalRi = Keeper.readContinueReachGoalRi();
//        ReportData i = DataManager.m12179a().m12230i();
//        if (!i.isValid()) {
//            GuliLog.tracePrintInfo("DynamicManager", "reportData is inValid");
//            z = true;
//        }
//        ReportInfo reportInfo = new ReportInfo();
//        reportInfo.dateTo = i.timeTo;
//        reportInfo.dateFrom = i.timeFrom;
//        GuliLog.tracePrintInfo("DynamicManager", "lastRi date from =" + readContinueReachGoalRi.dateFrom + ", lastReport.timeTo = " + readContinueReachGoalRi.dateTo);
//        GuliLog.tracePrintInfo("DynamicManager", "report date from =" + i.timeFrom + ", reportData.timeTo = " + i.timeTo);
//        if (z || !readContinueReachGoalRi.dateFrom.equals(i.timeFrom) || !readContinueReachGoalRi.dateTo.equals(i.timeTo)) {
//            GuliLog.tracePrintInfo("DynamicManager", i.toString());
//            Keeper.keepContinueReachGoalRi(reportInfo);
//            GuliLog.tracePrintInfo("DynamicManager", "continue reach goal report .........................");
//            LuaEvent.getInstance(context).setContinueReport(i);
//        }
    }

    private void m7898e(Context context) {
        DataManager a = DataManager.m12179a();
        DaySportData g = a.m9052g(a.m9057n());
        LuaEvent instance = LuaEvent.getInstance(context);
        if (g != null) {
            StepsInfo stepsInfo = g.getStepsInfo();
            if (stepsInfo != null) {
                int stepsCount = stepsInfo.getStepsCount();
                if (new SportDay().addDay(-7).compare(DataManager.m12179a().m9054k()) > 0) {
//                    ReportData h = DataManager.m12179a().m12227h();
//                    GuliLog.tracePrintInfo("DynamicManager", h.toString());
//                    if (stepsCount > h.steps) {
//                        GuliLog.tracePrintInfo("DynamicManager", "newRecordReport .........................");
//                        h.setStepsInfo(stepsInfo);
//                        instance.setRecord(h);
//                        return;
//                    }
                    return;
                }
                GuliLog.tracePrintInfo("DynamicManager", "newRecordReport do nothing.........................");
            }
        }
    }

    private void m7899f(Context context) {
        DataManager a = DataManager.m12179a();
        DaySportData g = a.m9052g(a.m9057n());
        LuaEvent instance = LuaEvent.getInstance(context);
        if (g != null) {
            SleepInfo sleepInfo = g.getSleepInfo();
            if (sleepInfo != null) {
                Calendar instance2 = Calendar.getInstance();
                if (instance2.get(Calendar.MINUTE) + (instance2.get(Calendar.HOUR_OF_DAY) * 60) > f3436e) {
                    GuliLog.tracePrintInfo("DynamicManager", "sleep report .........................");
                    instance.setSleep(sleepInfo);
                }
            }
        }
    }

    private void m7900g(Context context) {
        DataManager a = DataManager.m12179a();
        DaySportData g = a.m9052g(a.m9057n());
        LuaEvent instance = LuaEvent.getInstance(context);
        if (g != null) {
            StepsInfo stepsInfo = g.getStepsInfo();
            if (stepsInfo != null) {
                ArrayList activeList = stepsInfo.getActiveList();
                GuliLog.tracePrintInfo("DynamicManager", "Dump ActiveList:");
                Iterator it = activeList.iterator();
                while (it.hasNext()) {
                    ActiveItem activeItem = (ActiveItem) it.next();
                    GuliLog.tracePrintInfo("DynamicManager", "ActiveItem:" + activeItem.toString());
                    GuliLog.tracePrintInfo("DynamicManager", "steps " + activeItem.steps + " startTime " + activeItem.start + " " + new Date((long) activeItem.start) + " runtime " + activeItem.activeTime);
                    if (activeItem.flag == 0) {
                        instance.setActivities(activeItem);
                    }
                }
            }
        }
    }

    public static DynamicManager getInstance() {
        if (f3437f == null) {
            f3437f = new DynamicManager();
        }
        return f3437f;
    }

    private void m7901h(Context context) {
        Object obj = 1;
        DataManager a = DataManager.m12179a();
        DaySportData g = a.m9052g(a.m9057n());
        LuaEvent instance = LuaEvent.getInstance(context);
        if (g != null) {
            StepsInfo stepsInfo = g.getStepsInfo();
            if (stepsInfo != null) {
                int c;
                if (ChartData.m8731a().m8820x()) {
                    c = ChartData.m8731a().m8787c();
                    if (c <= 0) {
                        c = stepsInfo.getStepsCount();
                    }
                } else {
                    c = stepsInfo.getStepsCount();
                }
                PersonInfo readPersonInfo = Keeper.readPersonInfo();
                int daySportGoalSteps = readPersonInfo.getDaySportGoalSteps();
                GuliLog.tracePrintInfo("DynamicManager", "steps:" + c + ",goal:" + daySportGoalSteps);
                Calendar instance2 = Calendar.getInstance();
                if ((instance2.get(Calendar.HOUR_OF_DAY) >= f3433a ? 1 : null) != null || c >= daySportGoalSteps) {
                    instance.setGoal(c, daySportGoalSteps);
                } else {
                    instance.deleteGoalAchievedMsg();
                }
                if ((instance2.get(Calendar.HOUR_OF_DAY) * 60) + instance2.get(Calendar.MINUTE) < f3434b) {
                    obj = null;
                }
                if (obj == null) {
                    m7908c(context);
                } else if (readPersonInfo.getMiliConfig().getDailySportNofity().equals(C0668a.fG)) {
                    obj = Keeper.readDailySportReport();
                    if (TextUtils.isEmpty((CharSequence)obj) || !obj.equals(new SportDay().toString())) {
                        m7893a(context, c, daySportGoalSteps);
                        Keeper.keepDailySportReport(new SportDay().toString());
                        m7902i(context);
                    }
                }
            }
        }
    }

    private void m7902i(Context context) {
        Calendar instance = Calendar.getInstance();
        instance.add(Calendar.DAY_OF_YEAR, 1);
        instance.set(Calendar.HOUR_OF_DAY, 0);
        instance.set(Calendar.MINUTE, 0);
        instance.set(Calendar.SECOND, 1);
        ((AlarmManager) context.getSystemService(Context.ALARM_SERVICE)).set(0, instance.getTimeInMillis(), PendingIntent.getBroadcast(context, instance.hashCode(), new Intent(Constant.aY), PendingIntent.FLAG_CANCEL_CURRENT));
    }

    private void m7903j(Context context) {
        if (DeviceSource.hasBindBracelet()) {
            DataManager a = DataManager.m12179a();
            DaySportData g = a.m9052g(a.m9057n());
            Calendar instance = Calendar.getInstance();
            if (g != null) {
                SleepInfo sleepInfo = g.getSleepInfo();
                if (sleepInfo != null) {
                    boolean hasSleep = sleepInfo.getHasSleep();
                    if (!hasSleep) {
                        GuliLog.m12370d("DynamicManager", "hasSleep = " + hasSleep);
                    } else if (sleepInfo.getStopDate() != null) {
                        Object obj = null;
                        int i = instance.get(Calendar.MINUTE) + (instance.get(Calendar.HOUR_OF_DAY) * 60);
                        if (i > DataManager.m12179a().m12238q() && i < f3435c) {
                            obj = 1;
                        }
                        if (obj == null) {
                            return;
                        }
                        if (Keeper.readPersonInfo().getMiliConfig().getDailySleepNofity()) {
                            obj = Keeper.readLastNotifySleepDate();
                            if (TextUtils.isEmpty((CharSequence)obj) || !obj.equals(new SportDay().toString())) {
                                Keeper.setLastNotifySleepDate(new SportDay().toString());
                                m7894a(context, C0893f.m9086a(context, sleepInfo.getSleepCount()));
                                return;
                            }
                            return;
                        }
                        m7909d(context);
                    }
                }
            }
        }
    }

    public void m7904a(Context context) {
        m7901h(context);
        m7897c(context, true);
    }

    public void m7905a(Context context, int i) {
        LuaEvent.getInstance(context).updateWeatherTips(i);
    }

    public void m7906a(Context context, LabFactoryActiveItem labFactoryActiveItem) {
        LuaEvent.getInstance(context).callLabFactoryActivities(labFactoryActiveItem);
    }

    public void analysisDynamic(Context context, boolean z) {
        m7895a(context, z);
        m7896b(context, z);
        m7899f(context);
        m7901h(context);
        m7900g(context);
        m7898e(context);
        m7897c(context, z);
        m7903j(context);
    }

    public void m7907b(Context context) {
        m7899f(context);
    }

    public void m7908c(Context context) {
        ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(65537);
    }

    public void m7909d(Context context) {
        ((NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE)).cancel(16);
    }
}
